package com.dc.controller;

import com.dc.base.aop.AopOperation;
import com.dc.base.baseController.BaseController;
import com.dc.base.em.ErrorMessageEnum;
import com.dc.base.em.RoleMenuEnum;
import com.dc.base.vo.BaseModel;
import com.dc.base.vo.BusinessException;
import com.dc.model.Classify;
import com.dc.model.User;
import com.dc.service.ClassifyService;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiImplicitParam;
import com.wordnik.swagger.annotations.ApiImplicitParams;
import com.wordnik.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

// 标明swagger的信息 用在请求的类上，表示对类的说明
@Api(value="分类模块")
// 前缀
@RequestMapping("/classify")
//标明控制类，被mapper扫描到
@Controller
public class ClassifyController extends BaseController {
    @Autowired
    ClassifyService classifyService;

    //  分页查询分类信息
    //  新增操作日志
    @AopOperation(desc = "查询分类",type="查询",menu = RoleMenuEnum.NO_205)
    //  添加api注解
    @ApiOperation(value="分页查询分类信息",httpMethod = "GET",notes = "查询")
    // 映射url
    @RequestMapping(value = "/selectPageClassify", method = RequestMethod.GET)
    // 将java对象转为json格式的数据。
    @ResponseBody
    // requesetparam将请求参数绑定到控制器的方法参数上（是springmvc中接收普通参数的注解）
    public BaseModel selectPageClassify(
            @RequestParam(value="curr_page",defaultValue = "1") int curr_page,
            @RequestParam(value="page_size",defaultValue = "10") int page_size,
            BaseModel baseModel,
            @RequestParam(value="classify_name",required = false)String classify_name,
            @RequestParam(value="memo",required = false)String memo
    ){
        // 返回服务层的分页查询方法
        return  classifyService.selectPageClassify(curr_page,page_size,baseModel,classify_name,memo);
    }

    // 操作日志注解，新增操作日志
    @AopOperation(desc = "新增分类",type="新增",menu = RoleMenuEnum.NO_205)
    // 新增分类信息
    @ApiOperation(value="分类新增",httpMethod = "POST",notes="新增")
    @RequestMapping(value="/addClassify", method = RequestMethod.POST)
    @ResponseBody
//    @ApiImplicitParams({
//            @ApiImplicitParam(value="分类名",name="classify_name",required = true,dataType = "String",paramType = "query"),
//            @ApiImplicitParam(value="描述",name="memo",required = true,dataType = "String",paramType = "query")
//    })
    public BaseModel addClassify(Classify classify, String birthdayStr, HttpServletRequest request, BaseModel baseModel)throws Exception{
        classify.setCreate_time(new Date());
        /* 获取session,判断是否登录 */
        HttpSession session=request.getSession();
        User currUser=(User)session.getAttribute("currentUser");
        if(currUser==null) {
            throw new BusinessException(ErrorMessageEnum.LOGIN_TIMEOUT);
        }
        return classifyService.addClassify(classify,baseModel);
    }

    // 操作日志注解，新增操作日志
    @AopOperation(desc = "修改分类信息",type="修改",menu = RoleMenuEnum.NO_205)
    // 修改分类信息
    @ApiOperation(value="分类修改",httpMethod = "POST",notes="修改")
    @RequestMapping(value="/updateClassify", method = RequestMethod.POST)
    @ResponseBody
//    @ApiImplicitParams({
//            @ApiImplicitParam(value="分类名",name="classify_name",required = true,dataType = "String",paramType = "query"),
//            @ApiImplicitParam(value="描述",name="memo",required = true,dataType = "String",paramType = "query")
//    })
    public BaseModel updateClassify(Classify classify,BaseModel baseModel,HttpServletRequest request)throws Exception {
        classify.setUpdate_time(new Date());
        // 获取登录信息
        HttpSession session=request.getSession();
        // 记住要用User
        User currentUser=(User) session.getAttribute("currentUser");
        if(currentUser==null){
            throw new BusinessException(ErrorMessageEnum.LOGIN_TIMEOUT);
        }
        return classifyService.updateClassify(classify,baseModel);
    }

    // 根据id查询分类信息
    @ApiOperation(value="分类查询",httpMethod = "GET",notes="查询")
    @RequestMapping(value="/selectClassifyById", method = RequestMethod.GET)
    @ResponseBody
    public BaseModel selectClassifyById(@RequestParam("classify_id")Integer classify_id,
                                BaseModel baseModel,
                                HttpServletRequest request)throws Exception{
        return classifyService.selectClassifyById(classify_id,baseModel);
    }

    // 操作日志注解，新增操作日志
    @AopOperation(desc = "删除分类",type="删除",menu = RoleMenuEnum.NO_205)
    // 删除分类信息(修改)
    @ApiOperation(value="分类删除",httpMethod = "POST",notes="删除")
    @RequestMapping(value="/deleteClassify/{ids}", method = RequestMethod.POST)
    @ResponseBody
    public BaseModel deleteClassify(@PathVariable(value="ids") String ids,
                                  BaseModel baseModel,
                                  HttpServletRequest request)throws Exception{
        HttpSession session=request.getSession();
        // 错误示范
        // Classify currentUser=(Classify)session.getAttribute("currentUser");
        User currentUser = (User)session.getAttribute("currentUser");
        if(currentUser==null){
            throw new BusinessException(ErrorMessageEnum.LOGIN_TIMEOUT);
        }
        return classifyService.deleteClassify(ids,baseModel);
    }
}
